package com.cunjin.dormitory.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.dormitory.dao.DormitoryDao;
import com.cunjin.dormitory.entity.DormitoryEntity;
import com.cunjin.dormitory.service.DormitoryService;
import com.cunjin.student.entity.StudentEntity;


@Service
public class DormitoryServiceImpl extends BaseServiceImpl<DormitoryEntity> implements DormitoryService {
	
	@Autowired
	private DormitoryDao dao;

	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryRenShu(DormitoryEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		DormitoryEntity user;
		try {
			user = dao.queryRenShu(entity);
			if(null !=user){
				int counter = user.getCounter();
				int nownum = user.getNowNum();
				
				if(counter>nownum){
					map.put("code", "200");
					map.put("jg", "ok");
				}else{
					map.put("code", "500");
					map.put("jg", nownum);
				}
			}else{
				map.put("code", "500");
				map.put("jg", "null");
			}
		} catch (Exception e) {
			map.put("code", "500");
			map.put("jg", "null");
			e.printStackTrace();
		}
		return map;
	}
	
	@Override
	public List<DormitoryEntity> getcommoBox() {
		
		List<DormitoryEntity> selectAll =null;
		try {
			selectAll = dao.myqueryOk();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return selectAll;
	}
	
	@Override
	public Map<String, Object> insert(DormitoryEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			 DormitoryEntity queryRenShu = dao.queryRenShu(entity);
		if(null !=queryRenShu){
				map.put("code",201);
				map.put("msg", "已经存在宿舍");
			}else{
				entity.setNowNum(0);
				
				@SuppressWarnings("unused")
				int ls=dao.insert(entity);
				map.put("msg","添加宿舍成功");
				map.put("code",200);
			}
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName) {
		
		//将分页的数据保存在map中
		Map<String, Object> pages=new HashMap<String, Object>();
		
		int before=(page-1)*rows;
		int after=rows;
		pages.put("before", before);
		pages.put("after", after);
		if(null !=searchName){
			pages.put("searchName", searchName+"%");
		}else{
			pages.put("searchName", null);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<StudentEntity> ls=dao.myqueryByPage(pages);
			map.put("total",dao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				dao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("cs", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 考虑重复性
	 */
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> update(DormitoryEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//排除更新的宿舍重复性
		Integer oldId = entity.getId();
		String oldname = entity.getName();
		DormitoryEntity oldentity = dao.selectByPrimaryKey(oldId);
		String olddormitory = oldentity.getName();
		//如果是两个相等没有修改宿舍名字
		if(oldname.equals(olddormitory)){
			dao.updateByPrimaryKeySelective(entity);
			map.put("code", 200);
			map.put("msg","更新成功");
			return map;
		}else{
			//修改了宿舍名称
			try {
				DormitoryEntity selectByName = dao.selectByName(oldname);
				//==null 说明没有这个宿舍
				if(null ==selectByName){
					dao.updateByPrimaryKeySelective(entity);
				}else{
					map.put("code", 201);
					map.put("msg", "该宿舍"+oldname+"已经存在");
					return map;
				}
			} catch (Exception e) {
				map.put("code", 500);
				map.put("msg","更新失败");
				e.printStackTrace();
				return map;
			}
		}
		return map;
		}

}
